<template>
  <div class="implementation-personnel-container">
    <el-card shadow="never">
      <template #header>
        <div class="card-header">
          <span>实施人员档案</span>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>
            新增人员
          </el-button>
        </div>
      </template>

      <!-- 搜索区域 -->
      <div class="search-box">
        <el-form :inline="true" :model="searchForm">
          <el-form-item label="姓名">
            <el-input 
              v-model="searchForm.name" 
              placeholder="请输入姓名"
              clearable
              style="width: 150px"
            />
          </el-form-item>
          <el-form-item label="角色">
            <el-select 
              v-model="searchForm.role" 
              placeholder="请选择角色"
              clearable
              style="width: 150px"
            >
              <el-option label="全部" value="" />
              <el-option label="项目经理" value="pm" />
              <el-option label="实施顾问" value="consultant" />
              <el-option label="技术顾问" value="tech" />
              <el-option label="培训讲师" value="trainer" />
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-select 
              v-model="searchForm.status" 
              placeholder="请选择状态"
              clearable
              style="width: 120px"
            >
              <el-option label="全部" value="" />
              <el-option label="在职" value="active" />
              <el-option label="离职" value="inactive" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">
              <el-icon><Search /></el-icon>
              查询
            </el-button>
            <el-button @click="handleReset">
              <el-icon><Refresh /></el-icon>
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 统计卡片 -->
      <el-row :gutter="20" style="margin-bottom: 20px">
        <el-col :span="6">
          <el-card shadow="hover">
            <el-statistic title="总人数" :value="statistics.total">
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-statistic>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card shadow="hover">
            <el-statistic title="在职人员" :value="statistics.active">
              <template #prefix>
                <el-icon style="color: #67C23A"><Check /></el-icon>
              </template>
            </el-statistic>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card shadow="hover">
            <el-statistic title="进行中项目" :value="statistics.projects">
              <template #prefix>
                <el-icon style="color: #409EFF"><Briefcase /></el-icon>
              </template>
            </el-statistic>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card shadow="hover">
            <el-statistic title="本月完成任务" :value="statistics.tasks">
              <template #prefix>
                <el-icon style="color: #E6A23C"><TrendCharts /></el-icon>
              </template>
            </el-statistic>
          </el-card>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table 
        :data="tableData" 
        border 
        stripe
        v-loading="loading"
        style="width: 100%"
      >
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column label="照片" width="80" align="center">
          <template #default="{ row }">
            <el-avatar :src="row.avatar">{{ row.name }}</el-avatar>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="姓名" width="100" />
        <el-table-column prop="role" label="角色" width="120" align="center">
          <template #default="{ row }">
            <el-tag :type="getRoleType(row.role)">{{ getRoleName(row.role) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号" width="130" />
        <el-table-column prop="email" label="邮箱" min-width="180" show-overflow-tooltip />
        <el-table-column prop="skills" label="专业技能" min-width="200" show-overflow-tooltip>
          <template #default="{ row }">
            <el-tag 
              v-for="skill in row.skills.slice(0, 3)" 
              :key="skill" 
              size="small"
              style="margin-right: 5px"
            >
              {{ skill }}
            </el-tag>
            <el-text v-if="row.skills.length > 3" size="small" type="info">
              +{{ row.skills.length - 3 }}
            </el-text>
          </template>
        </el-table-column>
        <el-table-column prop="projectCount" label="项目数" width="90" align="center" />
        <el-table-column prop="level" label="级别" width="100" align="center">
          <template #default="{ row }">
            <el-rate v-model="row.level" disabled show-score text-color="#ff9900" />
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template #default="{ row }">
            <el-tag :type="row.status === 'active' ? 'success' : 'info'">
              {{ row.status === 'active' ? '在职' : '离职' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280" align="center" fixed="right">
          <template #default="{ row }">
            <el-button 
              type="primary" 
              size="small" 
              link
              @click="handleView(row)"
            >
              <el-icon><View /></el-icon>
              查看
            </el-button>
            <el-button 
              type="primary" 
              size="small" 
              link
              @click="handleEdit(row)"
            >
              <el-icon><Edit /></el-icon>
              编辑
            </el-button>
            <el-button 
              type="danger" 
              size="small" 
              link
              @click="handleDelete(row)"
            >
              <el-icon><Delete /></el-icon>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="800px"
      :close-on-click-modal="false"
    >
      <el-form 
        ref="formRef" 
        :model="form" 
        :rules="rules" 
        label-width="100px"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色" prop="role">
              <el-select v-model="form.role" placeholder="请选择角色" style="width: 100%">
                <el-option label="项目经理" value="pm" />
                <el-option label="实施顾问" value="consultant" />
                <el-option label="技术顾问" value="tech" />
                <el-option label="培训讲师" value="trainer" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="专业技能" prop="skills">
          <el-select 
            v-model="form.skills" 
            multiple 
            placeholder="请选择专业技能"
            style="width: 100%"
          >
            <el-option label="ERP实施" value="erp" />
            <el-option label="需求分析" value="requirement" />
            <el-option label="系统配置" value="config" />
            <el-option label="数据迁移" value="migration" />
            <el-option label="用户培训" value="training" />
            <el-option label="技术支持" value="support" />
            <el-option label="项目管理" value="pm" />
            <el-option label="二次开发" value="develop" />
          </el-select>
        </el-form-item>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="级别">
              <el-rate v-model="form.level" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio value="active">在职</el-radio>
                <el-radio value="inactive">离职</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="入职日期">
          <el-date-picker
            v-model="form.joinDate"
            type="date"
            placeholder="选择入职日期"
            style="width: 100%"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>

        <el-form-item label="个人简介">
          <el-input 
            v-model="form.bio" 
            type="textarea"
            :rows="4"
            placeholder="请输入个人简介"
            maxlength="500"
            show-word-limit
          />
        </el-form-item>

        <el-form-item label="备注">
          <el-input 
            v-model="form.remark" 
            type="textarea"
            :rows="2"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="submitLoading">
          确定
        </el-button>
      </template>
    </el-dialog>

    <!-- 查看详情对话框 -->
    <el-dialog
      v-model="detailDialogVisible"
      title="人员详情"
      width="800px"
    >
      <el-descriptions :column="2" border v-if="selectedPerson">
        <el-descriptions-item label="姓名">{{ selectedPerson.name }}</el-descriptions-item>
        <el-descriptions-item label="角色">
          <el-tag :type="getRoleType(selectedPerson.role)">
            {{ getRoleName(selectedPerson.role) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="手机号">{{ selectedPerson.phone }}</el-descriptions-item>
        <el-descriptions-item label="邮箱">{{ selectedPerson.email }}</el-descriptions-item>
        <el-descriptions-item label="级别">
          <el-rate v-model="selectedPerson.level" disabled />
        </el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="selectedPerson.status === 'active' ? 'success' : 'info'">
            {{ selectedPerson.status === 'active' ? '在职' : '离职' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="项目数量">{{ selectedPerson.projectCount }}</el-descriptions-item>
        <el-descriptions-item label="入职日期">{{ selectedPerson.joinDate }}</el-descriptions-item>
        <el-descriptions-item label="专业技能" :span="2">
          <el-tag 
            v-for="skill in selectedPerson.skills" 
            :key="skill"
            style="margin-right: 5px; margin-bottom: 5px"
          >
            {{ skill }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="个人简介" :span="2">
          {{ selectedPerson.bio }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  Plus, Search, Refresh, Edit, Delete, View, User, Check, Briefcase, TrendCharts
} from '@element-plus/icons-vue'

// 统计数据
const statistics = reactive({
  total: 0,
  active: 0,
  projects: 0,
  tasks: 0
})

// 搜索表单
const searchForm = reactive({
  name: '',
  role: '',
  status: ''
})

// 表格数据
const tableData = ref([])
const loading = ref(false)

// 分页
const pagination = reactive({
  page: 1,
  size: 10,
  total: 0
})

// 对话框
const dialogVisible = ref(false)
const dialogTitle = ref('新增人员')
const formRef = ref()
const submitLoading = ref(false)

// 详情对话框
const detailDialogVisible = ref(false)
const selectedPerson = ref(null)

// 表单数据
const form = reactive({
  id: null,
  name: '',
  role: '',
  phone: '',
  email: '',
  skills: [],
  level: 3,
  status: 'active',
  joinDate: '',
  bio: '',
  remark: ''
})

// 表单验证规则
const rules = {
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  role: [{ required: true, message: '请选择角色', trigger: 'change' }],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱', trigger: 'blur' }
  ],
  skills: [{ required: true, message: '请选择专业技能', trigger: 'change' }]
}

// 获取角色类型
const getRoleType = (role) => {
  const typeMap = {
    pm: 'danger',
    consultant: 'primary',
    tech: 'success',
    trainer: 'warning'
  }
  return typeMap[role] || ''
}

// 获取角色名称
const getRoleName = (role) => {
  const nameMap = {
    pm: '项目经理',
    consultant: '实施顾问',
    tech: '技术顾问',
    trainer: '培训讲师'
  }
  return nameMap[role] || role
}

// 查询
const handleSearch = () => {
  pagination.page = 1
  loadData()
}

// 重置
const handleReset = () => {
  searchForm.name = ''
  searchForm.role = ''
  searchForm.status = ''
  handleSearch()
}

// 新增
const handleAdd = () => {
  dialogTitle.value = '新增人员'
  dialogVisible.value = true
}

// 编辑
const handleEdit = (row) => {
  dialogTitle.value = '编辑人员'
  Object.assign(form, { ...row })
  dialogVisible.value = true
}

// 查看
const handleView = (row) => {
  selectedPerson.value = { ...row }
  detailDialogVisible.value = true
}

// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm(`确定要删除"${row.name}"吗？`, '提示', {
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
    loadData()
  }).catch(() => {})
}

// 提交
const handleSubmit = async () => {
  if (!formRef.value) return
  await formRef.value.validate((valid) => {
    if (valid) {
      submitLoading.value = true
      setTimeout(() => {
        ElMessage.success(form.id ? '更新成功' : '新增成功')
        dialogVisible.value = false
        submitLoading.value = false
        loadData()
      }, 1000)
    }
  })
}

// 分页改变
const handleSizeChange = (size) => {
  pagination.size = size
  loadData()
}

const handleCurrentChange = (page) => {
  pagination.page = page
  loadData()
}

// 加载数据
const loadData = async () => {
  loading.value = true
  try {
    await new Promise(resolve => setTimeout(resolve, 500))
    
    tableData.value = [
      {
        id: 1,
        name: '张伟',
        role: 'pm',
        phone: '13800138001',
        email: 'zhangwei@example.com',
        avatar: '',
        skills: ['ERP实施', '项目管理', '需求分析', '技术支持'],
        projectCount: 8,
        level: 5,
        status: 'active',
        joinDate: '2020-01-15',
        bio: '10年ERP实施经验，擅长大型企业ERP项目管理'
      },
      {
        id: 2,
        name: '李娜',
        role: 'consultant',
        phone: '13800138002',
        email: 'lina@example.com',
        avatar: '',
        skills: ['系统配置', '数据迁移', '用户培训'],
        projectCount: 12,
        level: 4,
        status: 'active',
        joinDate: '2019-06-20',
        bio: '资深实施顾问，专注于制造业ERP实施'
      },
      {
        id: 3,
        name: '王强',
        role: 'tech',
        phone: '13800138003',
        email: 'wangqiang@example.com',
        avatar: '',
        skills: ['二次开发', '技术支持', '系统配置'],
        projectCount: 15,
        level: 5,
        status: 'active',
        joinDate: '2018-03-10',
        bio: '技术专家，精通ERP系统架构和二次开发'
      },
      {
        id: 4,
        name: '赵敏',
        role: 'trainer',
        phone: '13800138004',
        email: 'zhaomin@example.com',
        avatar: '',
        skills: ['用户培训', '需求分析', 'ERP实施'],
        projectCount: 20,
        level: 4,
        status: 'active',
        joinDate: '2019-09-01',
        bio: '专业培训讲师，擅长用户培训和知识转移'
      }
    ]
    
    pagination.total = tableData.value.length
    
    // 更新统计数据
    statistics.total = tableData.value.length
    statistics.active = tableData.value.filter(item => item.status === 'active').length
    statistics.projects = tableData.value.reduce((sum, item) => sum + item.projectCount, 0)
    statistics.tasks = 156
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.implementation-personnel-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-box {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>

